import QtQuick 2.0
import QtQuick.Controls 1.2
import QtQuick.Window 2.0
//import SceneGraphRendering 1.0
import "../javascript/Utils.js" as UtilsJS

import Utils 1.0

Rectangle {
    property string titulo: "Mapa"
    property var subTitulo: null
    property bool showMenuButton: true

    id: mapaRoot
    width: parent.width
    height: Screen.height*90/100;

    property var mapaContent: null
    property bool initialized: false
    property var toAdd: new Array()

    property Rectangle toolBarMenu: Rectangle {
        width: UtilsJS.dp2px(150)
        height: menuItems.height
        color: "#f3f3f3"

        Column {
            id: menuItems
            width: parent.width

            Button {
                text: "Info"
                style: applicationWindow.botoesDrawer
                width: parent.width
                height: UtilsJS.dp2px(48)
                property string icon: ""
                onClicked: {
                    applicationWindow.hideToolBarMenu();
                    applicationWindow.showPath();
                }
            }
        }
    }
/*
    WebviewUtils {
        id: wvUtils
        onRequestReceived: {
            var index = req.indexOf(":");

            var key = req.substring(0,index);
            var val = req.substring(index+1);

            if(key=="markerClicked") {
                index = val.indexOf(",");
                var id = val.substring(0,index);
                var nome = val.substring(index+1);
                applicationWindow.exibeProdutos(id,nome);
            } else if(key=="newPos") {
                applicationWindow.setCurrentPos(val);
                if(!initialized) {
                    initialized = true;
                    var i;
                    for(i=0; i<toAdd.length; i++) {
                        addMarker(toAdd[i]["id"],toAdd[i]["lat"],toAdd[i]["lon"],
                                  toAdd[i]["color"],toAdd[i]["nome"]);
                    }
                }
            }
        }
    }

    OffscreenWebView {
        id: webView
        width: parent.width
        height: parent.height
        property bool startPage: true

        Component.onCompleted: {
            mapaRoot.mapaContent = wvUtils.readTextFile(":///html/map.html");
            webView.loadData(mapaRoot.mapaContent);
        }

        onPageStarted: {
            if(url.indexOf("data:")==0) {
                startPage = true;
                mapaRoot.initialized = false;
                mapaRoot.toAdd = new Array();
                addMarkers(fornecedoresController.loadDadosFornecedores());
            } else {
                startPage = false;
            }
        }

        function goBack() {
            webView.loadUrl("javascript:window.history.back();");
        }
    }

    function addMarker(id, lat, lon, color, text) {
        webView.loadUrl("javascript:addPin(" + id + "," + lat + "," + lon + ",'" + color + "','" + text + "');");
    }

    function addMarkers(estabelecimentos) {
        var i;
        for(i=0; i<estabelecimentos.length; i++) {
            if(!mapaRoot.initialized) {
                toAdd[toAdd.length] = estabelecimentos[i];
            } else {
                addMarker(estabelecimentos[i]["id"],estabelecimentos[i]["lat"],estabelecimentos[i]["lon"],
                          estabelecimentos[i]["color"],estabelecimentos[i]["nome"]);
            }
        }
    }

    function reload() {
        webView.loadData(mapaRoot.mapaContent);
    }

    function doUpdate() {
        reload();
    }

    function back() {
        if(webView.startPage) return true;

        webView.goBack();
        return false;
    }
*/

    function addMarker(id, lat, lon, color, text) {
    }

    function addMarkers(estabelecimentos) {
    }

    function doUpdate() {
    }

    function back() {
        return true;
    }
}
